// pages/orderdetail/orderdetail.js
var common = require("../../public");
var url = common.url;
var APP_ID = common.APP_ID;
var md5 = common.md5Start;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        delmask: false, //是否显示删除订单弹框
		countDownHour: '00',//倒计时小时
		countDownMinute: '00',//倒计时分钟
		countDownSecond: '00',//倒计时秒
    },
	//立即支付按钮
	pay_now:function(e){
		var id = this.data.id;//订单id
		wx.navigateTo({
			url: '../order_confirm/order_confirm?orderid='+id,
		})
	},
	//删除订单
	del_order:function(e){
		var id = this.data.id;//订单id
		var session_str = wx.getStorageSync('session_str');//获取密钥
		wx.showLoading({
			title: '加载中',
			mask: true,
		})
		wx.request({
			url: url + 'Order/deleteOrder',
			data: {
				session_str: session_str,
				order_id: id,
				APP_ID: APP_ID,
				sign: md5([
					"session_str" + session_str,
					"order_id" + id,
					"APP_ID" + APP_ID,
				])
			},
			header: {},
			method: 'post',
			dataType: 'json',
			responseType: 'text',
			success: function (res) {
				wx.hideLoading();
				if (res.data.code == 200) {
					that.setData({
						delmask: false,
					})
					wx.showToast({
						title: '操作成功',
						icon: 'success',
						mask: true,
					})
					setTimeout(function(){
						wx.navigateBack({
							delta: 1,
						})
					},1500)

				} else {
					wx.showToast({
						title: res.data.msg,
						icon: 'none',
						mask: true,
					})
				}
			},
			fail: function (res) {
				wx.hideLoading();
				wx.showToast({
					title: '网络错误',
					icon: 'none',
					mask: true,
				})
			}
		});
	},
	//取消订单
	cancelorder:function(e){
		var id = this.data.id;//订单id
		var session_str = wx.getStorageSync('session_str');//获取密钥
		wx.showModal({
			content: '确定取消订单？',
			showCancel: true,
			cancelText: '取消',
			confirmText: '确定',
			success: function(res) {
				if(res.confirm){
					wx.request({
						url: url + 'Order/cancelOrder',
						data: {
							session_str: session_str,
							order_id: id,
							APP_ID: APP_ID,
							sign: md5([
								"session_str" + session_str,
								"order_id" + id,
								"APP_ID" + APP_ID,
							])
						},
						header: {},
						method: 'post',
						dataType: 'json',
						responseType: 'text',
						success: function (res) {
							wx.hideLoading();
							if (res.data.code == 200) {
								wx.showToast({
									title: '操作成功',
									icon: 'success',
									mask: true,
								})
								setTimeout(function () {
									wx.navigateBack({
										delta: 1,
									})
								}, 1500)
							} else {
								wx.showToast({
									title: res.data.msg,
									icon: 'none',
									mask: true,
								})
							}
						},
						fail: function (res) {
							wx.hideLoading();
							wx.showToast({
								title: '网络错误',
								icon: 'none',
								mask: true,
							})
						}
					});
				}
			},
		})
		
		
	},
    countdown:function(that) {
		let endtime = that.data.endtime;
		setInterval(function () {
			let nowtime = Date.parse(new Date()) / 1000;
			if ((endtime - nowtime) >= 0) {
				let timer = endtime - nowtime;
				let d = parseInt(timer / 60 / 60 / 60 / 24, 10);//计算剩余小时
				let h = parseInt(timer / 60 / 60 % 24, 10);//计算剩余小时
				let m = parseInt(timer / 60 % 60, 10);//计算剩余分钟
				let s = parseInt(timer % 60, 10);//计算剩余秒数
				h = h < 10 ? h : h;
				m = m < 10 ? m : m;
				s = s < 10 ? s : s;
				that.setData({
					days: d,
					hours: h,
					minutes: m,
					seconds: s,
				})
			} else {
				that.setData({

				})
			}
		}, 1000)
        
        
    },
    //显示 删除订单弹框
    show_delmask: function(e) {
        var that = this;
        that.setData({
            delmask: true
        })
    },
    //关闭 删除订单弹框
    closeDelAlert: function(e) {
        this.setData({
            delmask: false
        })
    },
    //点击删除订单按钮
    delorder: function(e) {
        this.setData({
            delmask: true
        })
    },
	//跳转 申请售后 页面
	go_shouhou:function(e){
		var id = this.data.id;//订单id
		wx.navigateTo({
			url: '../shouhou_apply/shouhou_apply?id='+id
		})
	},
	//确认收货
	confirm_receive:function(e){
		var that = this;
		var id = this.data.id;//订单id
		var session_str = wx.getStorageSync('session_str');//获取密钥
		wx.showModal({
			content: '确认收货？',
			showCancel: true,
			cancelText: '取消',
			confirmText: '确定',
			success: function (res) {
				if (res.confirm) {
					wx.request({
						url: url + 'Order/receipt',
						data: {
							session_str: session_str,
							order_id: id,
							APP_ID: APP_ID,
							sign: md5([
								"session_str" + session_str,
								"order_id" + id,
								"APP_ID" + APP_ID,
							])
						},
						header: {},
						method: 'post',
						dataType: 'json',
						responseType: 'text',
						success: function (res) {
							wx.hideLoading();
							if (res.data.code == 200) {
								wx.showToast({
									title: '操作成功',
									icon: 'success',
									mask: true,
								})
								setTimeout(function(){
									wx.navigateBack({
										delta: 1,
									})
								},1500)
							} else {
								wx.showToast({
									title: res.data.msg,
									icon: 'none',
									mask: true,
								})
							}
						},
						fail: function (res) {
							wx.hideLoading();
							wx.showToast({
								title: '网络错误',
								icon: 'none',
								mask: true,
							})
						}
					});
				}
			},
		})
	},

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
		var that = this;
		var second = '1811111';
		this.setData({
			second: second
		})
		this.setData({
			id:options.id,//订单id
		})
        var wordflow = options.wordflow;
        wordflow = 1;
        if (wordflow == 1) {
            wx.setNavigationBarTitle({
                title: '待发货'
            })
        }
		that.countdown(that);

	},

    /**
     * 格式化时间
     */
	getFormat: function (msec) {
		let ss = parseInt(msec / 1000);
		let ms = parseInt(msec % 1000);
		let mm = 0;
		let hh = 0;
		if (ss > 60) {
			mm = parseInt(ss / 60);
			ss = parseInt(ss % 60);
			if (mm > 60) {
				hh = parseInt(mm / 60);
				mm = parseInt(mm % 60);
			}
		}
		ss = ss > 9 ? ss : `0${ss}`;
		mm = mm > 9 ? mm : `0${mm}`;
		hh = hh > 9 ? hh : `0${hh}`;
		return {
			ms,
			ss,
			mm,
			hh
		};
	}, 

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

	},
	// 倒计时（热门推荐)
	setCountDown_hot: function (countDown) {
		let timer_hot = this.data.timer_hot;
		if (timer_hot) {
			clearTimeout(timer_hot);
		}
		let time = 1000;

		let {
			show_daojishi
		} = this.data;
		if (countDown) {
			let formatTime = this.getFormat(countDown);
			countDown -= time;
			show_daojishi = `${formatTime.hh}:${formatTime.mm}:${formatTime.ss}`;
		} else {
			show_daojishi = '00:00:00'; //============做判断   如果没有倒计时的话
		}
		this.setData({
			show_daojishi: show_daojishi
		});
		timer_hot = setTimeout(this.setCountDown_hot, time);
		this.setData({
			timer_hot: timer_hot
		})
	},

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
		var that = this;
		var id = this.data.id;//订单id
		var session_str = wx.getStorageSync('session_str');//获取密钥
		wx.request({
			url: url + 'Order/orderInfo',
			data: {
				session_str: session_str,
				order_id: id,
				APP_ID: APP_ID,
				sign: md5([
					"session_str" + session_str,
					"order_id" + id,
					"APP_ID" + APP_ID,
				])
			},
			header: {},
			method: 'post',
			dataType: 'json',
			responseType: 'text',
			success: function (res) {
				wx.hideLoading();
				if (res.data.code == 200) {
					that.setData({
						orderinfo: res.data.data,//订单数据
					})
					if(res.data.data.workflow==0){
						var daojishi = res.data.data.last_pay_time;
						var now_timstamp = Date.parse(new Date());//现在的时间戳
						var totalSecond = (daojishi * 1000 - now_timstamp)/1000;//支付结束时间-现在的时间		
						console.log(now_timstamp)
						console.log(totalSecond)
						var interval = setInterval(function () {
							// 秒数
							var second = totalSecond;
							// 小时位
							var hr = Math.floor(second / 3600);
							var hrStr = hr.toString();
							if (hrStr.length == 1) hrStr = '0' + hrStr;

							// 分钟位
							var min = Math.floor((second -  hr * 3600) / 60);
							var minStr = min.toString();
							if (minStr.length == 1) minStr = '0' + minStr;

							// 秒位
							var sec = second -  hr * 3600 - min * 60;
							var secStr = sec.toString();
							if (secStr.length == 1) secStr = '0' + secStr;

							that.setData({
								countDownHour: hrStr,
								countDownMinute: minStr,
								countDownSecond: secStr,
							});
							totalSecond--;
							if (totalSecond < 0) {
								clearInterval(interval);
								that.setData({
									countDownHour: '00',
									countDownMinute: '00',
									countDownSecond: '00',
								});
							}
						}.bind(this), 1000)
					}
					if (res.data.data.status == 1 && res.data.data.workflow == 0) {
						//待发货
						wx.setNavigationBarTitle({
							title: '待付款',
						})
					}
					if (res.data.data.status == 1 && res.data.data.workflow==1){
						//待发货
						wx.setNavigationBarTitle({
							title: '待发货',
						})
					}
					if (res.data.data.status == 1 && res.data.data.workflow == 2) {
						//待收货
						wx.setNavigationBarTitle({
							title: '待收货',
						})
					}
					if (res.data.data.status == 1 && res.data.data.workflow == 3) {
						//待发货
						wx.setNavigationBarTitle({
							title: '已完成',
						})
					}

					if (res.data.data.status == 2 && res.data.data.workflow == 1) {
						//待发货
						wx.setNavigationBarTitle({
							title: '已取消',
						})
					}
				} else {
					wx.showToast({
						title: res.data.msg,
						icon: 'none',
						mask: true,
					})
				}
			},
			fail: function (res) {
				wx.hideLoading();
				wx.showToast({
					title: '网络错误',
					icon: 'none',
					mask: true,
				})
			}
		});
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})